package io;

/* version 1
 * by: Mike
 */

import ioProcessor.ProgramInputProcessor;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class ProgramInput {

	public static FileInputStream programInput;
	public static ProgramInputProcessor pip;
	public static String pInput = "by_luck_value";

	static void openFile() {
		System.out.print("Please enter the path of the program file: ");
		Scanner input = new Scanner(System.in);
		File programFile = new File(input.next());
		try {
			programInput = new FileInputStream(programFile);
			System.out.println("File open.");
			// fileExists = true;
		} catch (FileNotFoundException e) {
			System.out.println("File not found. " + e);
			// fileExists = false;
		}
	}

	public static void programDataFromFile() {
		// boolean fileExists = false;
		// FileInputStream programInput = null;

		while (programInput == null) {
			openFile();
		}

		DataInputStream programData = null;
		BufferedReader programReader = null;

		programData = new DataInputStream(programInput);
		programReader = new BufferedReader(new InputStreamReader(programData));

		if (pip == null)
			pip = new ProgramInputProcessor();
		try {
			while ((pInput = programReader.readLine()) != null) {
				pip.processProgramData(pInput);
			}
		} catch (IOException e) {
			System.out.println("Exception while reading the file " + e);
		}

		try {
			programData.close();
			programReader.close();
			programInput.close();
		} catch (IOException e) {
			System.err.println("Error: " + e.getMessage());
		}
	}
}
